Add Two Numbers

Medium

Extra practice. This problem has no walkthrough slides. Try solving it with the pattern template on your own, and lean on the hints if you get stuck.

Question

You're given two linked lists, l1 and l2, where each node holds a single digit of a non-negative integer. The digits are stored in reverse order, so the head of each list holds the ones digit.

Add the two numbers together and return the sum as a linked list in the same reverse-digit format.

Note: Neither input list is empty, but they may have different lengths, and neither number has a leading zero unless the number itself is 0.

Input: l1 = [2 -> 4 -> 3], l2 = [5 -> 6 -> 4]

Output: [7 -> 0 -> 8]

342 + 465 = 807, stored as [7 -> 0 -> 8].

Input: l1 = [5], l2 = [5]

Output: [0 -> 1]

5 + 5 = 10, stored as [0 -> 1].

Input: l1 = [9 -> 9 -> 9], l2 = [1]

Output: [0 -> 0 -> 0 -> 1]

999 + 1 = 1000, stored as [0 -> 0 -> 0 -> 1].

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

What does adding l1 = [8 -> 9] and l2 = [1] produce? Remember, digits are stored ones-first.
[9 -> 9]
[9 -> 0 -> 1]
[8, 10]
[0 -> 0 -> 1]

Take a moment to understand the problem and think of your approach before you start coding.